package com.example.studymvvmproject01.ui

import android.util.Log
import com.example.studymvvmproject01.base.BaseVmActivity
import com.example.studymvvmproject01.databinding.ActivityMain1Binding
import com.example.studymvvmproject01.login.LoginBean
import com.example.studymvvmproject01.login.LoginViewModel
import com.example.studymvvmproject01.utils.MyConfig
import com.example.studymvvmproject01.utils.RequestUtil
import com.example.studymvvmproject01.utils.SpUtil
import java.util.*

class LoginActivity : BaseVmActivity<ActivityMain1Binding, LoginViewModel>() {
    override fun viewModelClass(): Class<LoginViewModel> {

        return LoginViewModel::class.java
    }

    override fun getViewBinding(): ActivityMain1Binding {
        return ActivityMain1Binding.inflate(layoutInflater)

    }

    override fun initData() {
        super.initData()
        //登录
        val hashMap = HashMap<String, String>()
        hashMap["scope"] = "server"
        hashMap["username"] = "66admin"
        hashMap["password"] = "Kyny@2021"
        hashMap["grant_type"] = "password"
        val requestBody = RequestUtil.getRequestBody(hashMap);
        mViewModel.login(requestBody)
        //登录返回结果数据订阅
        mViewModel.workIndex(1)

    }

    override fun initView() {
        super.initView()

        mViewModel.loginInfo.observe(this, {
            mBinding?.tvContent?.text = it.getAccess_token() + "," + it.getUsername()
            SpUtil.setString(MyConfig.COOKIE, it.getAccess_token())
        })

        mViewModel.homeProadBean.observe(this,{
            it.getMsg()?.let { it1 -> Log.e("TAG", it1) }
            for(data in it.getData()!!){
                Log.e("data", data?.personCnt.toString())
            }
        })

    }

}